<?php
#                                                                                 
# author: Cody Sorgenfrey                                       
# (c)2013 South Hill Calvary Chapel                                               
#                                                                                 
include_once("../mysql/database_info.php");
include_once("../mysql/common.php");
include_once("../common.php");
connect_select_test("events");
if (empty($_GET['id'])){
	return 0;
} else{
	$id = $_GET['id'];
}
connect_select_test("events");
$eventQuery = mysql_query("SELECT * FROM `events` WHERE `id`='".$id."'");
$eventArray = mysql_fetch_array($eventQuery);
$formQuestionsQuery = mysql_query("SELECT * FROM `registrationForms` WHERE `event` LIKE '".$id."%'");
while ($form = mysql_fetch_array($formQuestionsQuery)){
	for ($x=0;$x<(count($form)/2);$x++){
		if (!empty($form[$x."Name"])){
			$formQuestionsArray[] = $form[$x."Placeholder"];
			$mysqlNamesArray[] = $form[$x."Name"];
		}
	}
	$formIds[] = $form['event'];
	$formNames[] = $form['formName'];
}
$dataArray = array_combine($mysqlNamesArray, $formQuestionsArray);
$formQuestionsArray = array_values($dataArray);
array_push($formQuestionsArray, "Payment Type");
array_push($formQuestionsArray, "Grand Total");
array_unshift($formQuestionsArray, "Scholarship");
array_unshift($formQuestionsArray, "Paid");
array_unshift($formQuestionsArray, "Id");
$mysqlNamesArray = array_keys($dataArray);
array_push($mysqlNamesArray, "paymentType");
array_push($mysqlNamesArray, "grandTotal");
array_unshift($mysqlNamesArray, "scholarship");
array_unshift($mysqlNamesArray, "paid");
array_unshift($mysqlNamesArray, "id");

for ($x=0;$x<count($formIds);$x++){
	for ($y=0;$y<count($mysqlNamesArray);$y++){
		$formRepliesQuery[$x][$y] = mysql_query("SELECT `".$mysqlNamesArray[$y]."` FROM `Registration_".$formIds[$x]."`");
		if ($formRepliesQuery[$x][$y]){
			for ($z=0;$z<mysql_num_rows($formRepliesQuery[$x][$y]);$z++){
				$formRepliesArray[$x][$y][$z] = mysql_result($formRepliesQuery[$x][$y], $z);
			}
		}
		
	}
}
if (empty($formRepliesArray)){
	return 0;
}
echo $eventArray['picture']."|row|";
echo $eventArray['title']."|row|";
for ($x=0;$x<count($formQuestionsArray);$x++){
	echo $formQuestionsArray[$x];
	echo "|cell|";
	echo $mysqlNamesArray[$x];
	if ($x != (count($formQuestionsArray)-1)){
		echo "|cell|";
	}
}
echo "|row|";
for ($x=0;$x<count($formIds);$x++){
	$a=0;
	while(count($formRepliesArray[$x][$a])==0){
		$a++;
	}
	$rowCount[$x] = count($formRepliesArray[$x][$a]);
	for ($y=0;$y<$rowCount[$x];$y++){
		echo $formIds[$x]."|cell|";
		for ($z=0;$z<count($formQuestionsArray);$z++){
			if ($formRepliesArray[$x][$z][$y] == null){
				$formRepliesArray[$x][$z][$y] = "N/A";
			}
			echo $formRepliesArray[$x][$z][$y];
			if ($z != (count($formQuestionsArray)-1)){
				echo "|cell|";
			} 
		}
		if ((($y+1) != $rowCount[$x]) || ((($y+1) == $rowCount[$x]) && ($x != (count($formIds)-1)))) {
			echo "|row|";
		} 
	}
}
?>